package com.example.lss.news.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lss.news.R;
import com.example.lss.news.utils.pic.GlideApp;
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;

public class VideoAdapter extends BaseAdapter {

    public final static String TAG = "TT22";

    private LayoutInflater inflater = null;
    private String[] url;
    private Context context;

    private ViewGroup rootView;

    private OrientationUtils orientationUtils;

    private boolean isFullVideo;

    private GSYVideoHelper smallVideoHelper;

    private GSYVideoHelper.GSYVideoHelperBuilder gsySmallVideoHelperBuilder;

    public VideoAdapter(Context context, GSYVideoHelper smallVideoHelper, GSYVideoHelper.GSYVideoHelperBuilder gsySmallVideoHelperBuilder, String[] url) {
        super();
        this.context = context;
        this.smallVideoHelper = smallVideoHelper;
        this.gsySmallVideoHelperBuilder = gsySmallVideoHelperBuilder;

        inflater = LayoutInflater.from(context);


        this.url = url;
    }

    String[] title = new String[]{"大话西游搞笑段子", "大话西游搞笑段子", "大话西游搞笑段子", "我就是国服冈本旭",
            "宝宝旭旭", "启明星的勾引", "旭旭宝宝", "旭旭宝宝查房记", "旭旭宝宝相亲记",};
    String[] png = new String[]{"http://wscdn.miaopai.com/xkx4/2015/1209/fa07d33e88882060dee6ac801d9941ff.png",
            "http://wscdn.miaopai.com/xkx4/2015/0714/749edd22cf9a24b168e53338ad5c399f.png",
            "http://wscdn.miaopai.com/xkx4/2015/0714/2841b3bd406e2dd594c7802fdf917927.png",
            "http://imgaliyuncdn.miaopai.com/images/N9A9EwF97LxsMBYph3~97ddLdqc9TARHJCSTMw___vigg.jpg",
            "http://imgaliyuncdn.miaopai.com/images/aciJTFQNgdLuMJ7pRSJx30gvgt7luMXpXKPJMA___wz27.jpg",
            "http://imgaliyuncdn.miaopai.com/images/D3sRA25VdDPM~HTfcV5CQ8uiJyIOP-fW4c0XMg___jhs7.jpg",
            "http://imgaliyuncdn.miaopai.com/images/IiicrmM7CGW~15fX0B0DG0tR4KHOC8J6QMfLsg___5v9c.jpg",
            "http://imgaliyuncdn.miaopai.com/images/clJuK8dkJ0fMx-AK1jljhs7oarPIQvZE1XySjw___4kcf.jpg",
            "http://imgaliyuncdn.miaopai.com/images/KM1oPGG4By~yaq7Y~QBtX4sqoRRW5OuMOk7tAQ___mt6v.jpg"};

    @Override
    public int getCount() {
        return url.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.video_view, null);
            holder.videoContainer = convertView.findViewById(R.id.list_item_container);
            holder.playerBtn = convertView.findViewById(R.id.list_item_btn);
            holder.tv = convertView.findViewById(R.id.tit);
            holder.imageView = new ImageView(context);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //增加封面
        holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        GlideApp.with(convertView).load(png[position]).into(holder.imageView);
        holder.tv.setText(title[position]);
        smallVideoHelper.addVideoPlayer(position, holder.imageView, TAG, holder.videoContainer, holder.playerBtn);
//        smallVideoHelper.isFull();
        holder.playerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notifyDataSetChanged();
                smallVideoHelper.setPlayPositionAndTag(position, TAG);
                gsySmallVideoHelperBuilder.setVideoTitle("title " + position)
                        .setUrl(url[position]);
                smallVideoHelper.startPlay();
            }
        });

        return convertView;
    }


    class ViewHolder {
        FrameLayout videoContainer;
        ImageView playerBtn;
        ImageView imageView;
        TextView tv;
    }

    public void setRootView(ViewGroup rootView) {
        this.rootView = rootView;
    }
}
